package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品品牌后台管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Resource
    private BrandService brandService;

    @GetMapping("/list")
    @Operation(summary = "分页查询分类品牌列表")
    @RequiresPermissions("product:brand:list")
    public TableDataInfo list(Brand brand) {
        startPage();
        String name = brand.getName();
        List<Brand> list = brandService.list(new LambdaQueryWrapper<Brand>().like(name != null, Brand::getName, name));
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    @Operation(summary = "查询分类品牌列表")
    @RequiresPermissions("product:brand:listAll")
    public AjaxResult list() {
        List<Brand> list = brandService.list();
        return success(list);
    }

    @InnerAuth
    @GetMapping("/getBrandAll")
    @Operation(summary = "内部调用接口：查询分类品牌列表")
    public R<List<Brand>> getBrandAll() {
        List<Brand> list = brandService.list();
        return R.ok(list);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id获取分类品牌详情")
    @RequiresPermissions("product:brand:info")
    public AjaxResult getBrandById(@PathVariable("id") Long id) {
        Brand brand = brandService.getById(id);
        return success(brand);
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "批量删除分类品牌")
    @RequiresPermissions("product:brand:remove")
    public AjaxResult deleteBrandByIds(@PathVariable("ids") List<Long> ids) {
        boolean batch = brandService.removeBatchByIds(ids);
        return toAjax(batch);
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增或修改分类品牌")
    @RequiresPermissions("product:brand:saveOrUpdate")
    public AjaxResult saveOrUpdate(@Validated @RequestBody Brand brand) {
        brandService.checkBrandName(brand);
        boolean flag = brandService.saveOrUpdate(brand);
        return toAjax(flag);
    }
}


